<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>动态规划（Dynamic Programming）（dp）描述 | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言动态规划（Dynamic Programming）（dp）描述。  一、核心概念 重叠局部问题 -&gt; 全局问题 状态推导   二、典型问题类型1：  统计数量&#x2F;方案类 求最优解类：背包问题  类型2：依据力扣分类  基础问题 背包问题 打家劫舍问题 股票问题 子序列问题  类型3：  线性 非线性   非线性树型如：337.打家劫舍 III - 力扣（LeetCode）——中等">
<meta property="og:type" content="article">
<meta property="og:title" content="动态规划（Dynamic Programming）（dp）描述">
<meta property="og:url" content="http://example.com/2023/02/01/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%88Dynamic%20Programming%EF%BC%89%EF%BC%88dp%EF%BC%89%E6%8F%8F%E8%BF%B0/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言动态规划（Dynamic Programming）（dp）描述。  一、核心概念 重叠局部问题 -&gt; 全局问题 状态推导   二、典型问题类型1：  统计数量&#x2F;方案类 求最优解类：背包问题  类型2：依据力扣分类  基础问题 背包问题 打家劫舍问题 股票问题 子序列问题  类型3：  线性 非线性   非线性树型如：337.打家劫舍 III - 力扣（LeetCode）——中等">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover18.png">
<meta property="article:published_time" content="2023-02-01T03:38:55.000Z">
<meta property="article:modified_time" content="2023-04-13T05:09:49.481Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover18.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2023/02/01/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%88Dynamic%20Programming%EF%BC%89%EF%BC%88dp%EF%BC%89%E6%8F%8F%E8%BF%B0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '动态规划（Dynamic Programming）（dp）描述',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-13 13:09:49'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover18.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">动态规划（Dynamic Programming）（dp）描述</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-02-01T03:38:55.000Z" title="发表于 2023-02-01 11:38:55">2023-02-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-13T05:09:49.481Z" title="更新于 2023-04-13 13:09:49">2023-04-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%AE%97%E6%B3%95/">算法</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="动态规划（Dynamic Programming）（dp）描述"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>动态规划（Dynamic Programming）（dp）描述。</p>
<hr>
<h1 id="一、核心概念"><a href="#一、核心概念" class="headerlink" title="一、核心概念"></a>一、核心概念</h1><ul>
<li>重叠局部问题 -&gt; 全局问题</li>
<li>状态推导</li>
</ul>
<hr>
<h1 id="二、典型问题"><a href="#二、典型问题" class="headerlink" title="二、典型问题"></a>二、典型问题</h1><p>类型1：</p>
<ul>
<li>统计数量&#x2F;方案类</li>
<li>求最优解类：背包问题</li>
</ul>
<p>类型2：依据力扣分类</p>
<ul>
<li>基础问题</li>
<li>背包问题</li>
<li>打家劫舍问题</li>
<li>股票问题</li>
<li>子序列问题</li>
</ul>
<p>类型3：</p>
<ul>
<li>线性</li>
<li>非线性</li>
</ul>
<blockquote>
<p>非线性树型如：337.打家劫舍 III - 力扣（LeetCode）——中等</p>
</blockquote>
<hr>
<h1 id="三、算法步骤"><a href="#三、算法步骤" class="headerlink" title="三、算法步骤"></a>三、算法步骤</h1><ol>
<li>确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果&#x2F;返回值</li>
<li>确定dp数组的状态转移方程&#x2F;递推公式</li>
<li>确定dp数组的初始化值</li>
<li>确定dp数组的遍历顺序和临界条件</li>
<li>模拟&#x2F;举例推导dp数组以验证</li>
</ol>
<blockquote>
<p>算法往往为多重循环而不容易模拟&#x2F;举例推导dp数组以验证。可在调试时再验证。</p>
</blockquote>
<hr>
<h1 id="四、模板示例"><a href="#四、模板示例" class="headerlink" title="四、模板示例"></a>四、模板示例</h1><p> 509.斐波那契数 - 力扣（LeetCode）——简单的题解：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Solution</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">fib</span><span class="params">(<span class="type">int</span> n)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (n == <span class="number">0</span> || n == <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> n;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">        <span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">dp</span><span class="params">(n + <span class="number">1</span>)</span></span>;</span><br><span class="line">        <span class="comment">// 3. 确定dp数组的初始化值</span></span><br><span class="line">        dp[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">        dp[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">2</span>; i &lt;= n; ++i)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">            dp[i] = dp[i - <span class="number">1</span>] + dp[i - <span class="number">2</span>];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">        <span class="keyword">return</span> dp[n];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 5. 模拟/举例推导dp数组以验证</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<hr>
<h1 id="五、调试方法"><a href="#五、调试方法" class="headerlink" title="五、调试方法"></a>五、调试方法</h1><ul>
<li>输出dp数组</li>
<li>判断是否和模拟&#x2F;举例推导的dp数组一致</li>
<li>若不一致，则可能是算法步骤问题</li>
<li>若一致，则可能是细节问题</li>
</ul>
<hr>
<h1 id="六、性能分析"><a href="#六、性能分析" class="headerlink" title="六、性能分析"></a>六、性能分析</h1><ul>
<li>时间复杂度：一般为多重循环的性能</li>
<li>空间复杂度：一般为dp数组的规模</li>
</ul>
<hr>
<h1 id="七、优化方法"><a href="#七、优化方法" class="headerlink" title="七、优化方法"></a>七、优化方法</h1><h2 id="1-记忆化搜索"><a href="#1-记忆化搜索" class="headerlink" title="1. 记忆化搜索"></a>1. 记忆化搜索</h2><p>作用：降低时间复杂度，增加空间复杂度</p>
<p>描述：</p>
<ul>
<li>使用数据结构存储已计算的状态</li>
<li>不再重复计算状态</li>
<li>从数据结构获取已计算的状态</li>
</ul>
<p>可能的优化：O(n²) -&gt; O(n)。n为数据规模</p>
<hr>
<h2 id="2-滚动数组"><a href="#2-滚动数组" class="headerlink" title="2. 滚动数组"></a>2. 滚动数组</h2><p>作用：优化空间复杂度</p>
<p>描述：</p>
<ul>
<li>状态推导时</li>
<li>若后一个状态<strong>只依赖固定的</strong>前一个或多个状态</li>
<li>则无需定义<strong>相应数据规模</strong>的dp数组</li>
<li>只需定义<strong>状态依赖规模</strong>的dp数组或变量</li>
<li>在递推中不断更新该dp数组或变量为前一个或多个<strong>所求解</strong>的状态，再用于求解后一个状态</li>
</ul>
<p>可能的优化：O(n) -&gt; O(1)；O(m × n) -&gt; O(n)</p>
<hr>
<h1 id="八、背包问题"><a href="#八、背包问题" class="headerlink" title="八、背包问题"></a>八、背包问题</h1><h2 id="1-类型"><a href="#1-类型" class="headerlink" title="1. 类型"></a>1. 类型</h2><p>混合背包：</p>
<ul>
<li>0-1背包 （基础）：各物品数量为一</li>
<li>完全背包：各物品数量不限</li>
<li>多重背包：各物品数量不同</li>
</ul>
<p>分组背包：依组装包，每组数量为一</p>
<hr>
<h2 id="2-算法步骤"><a href="#2-算法步骤" class="headerlink" title="2. 算法步骤"></a>2. 算法步骤</h2><ol start="0">
<li>分析应用问题转换为背包问题 -&gt; 确定背包问题的参数</li>
</ol>
<blockquote>
<p>背包：容量<br>物品：体积，价值，数量</p>
</blockquote>
<ol>
<li>确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果&#x2F;返回值</li>
<li>确定dp数组的状态转移方程&#x2F;递推公式</li>
<li>确定dp数组的初始化值</li>
<li>确定dp数组的遍历顺序和临界条件</li>
<li>确定dp数组的返回值</li>
<li>模拟&#x2F;举例推导dp数组以验证</li>
</ol>
<hr>
<h2 id="3-模板示例：0-1背包问题"><a href="#3-模板示例：0-1背包问题" class="headerlink" title="3. 模板示例：0-1背包问题"></a>3. 模板示例：0-1背包问题</h2><blockquote>
<p>详细解释参见：<a target="_blank" rel="noopener" href="https://programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-1.html">代码随想录 (programmercarl.com)</a></p>
</blockquote>
<p> 二维dp数组：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">func</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 0. 分析应用问题转换为背包问题 -&gt; 确定背包问题的参数</span></span><br><span class="line">    <span class="type">int</span> capacity = <span class="number">4</span>;                 <span class="comment">// 背包：容量</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; weight = &#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>&#125;;   <span class="comment">// 物体：重量</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; value = &#123;<span class="number">15</span>, <span class="number">20</span>, <span class="number">30</span>&#125;; <span class="comment">// 物体：价值</span></span><br><span class="line">    <span class="type">int</span> quantity = <span class="number">3</span>;                 <span class="comment">// 物体：数量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">    vector&lt;vector&lt;<span class="type">int</span>&gt;&gt; <span class="built_in">dp</span>(quantity, <span class="built_in">vector</span>&lt;<span class="type">int</span>&gt;(capacity + <span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 确定dp数组的初始化值</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; weight[<span class="number">0</span>]; ++j)</span><br><span class="line">    &#123;</span><br><span class="line">        dp[<span class="number">0</span>][j] = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j = weight[<span class="number">0</span>]; j &lt;= capacity; ++j)</span><br><span class="line">    &#123;</span><br><span class="line">        dp[<span class="number">0</span>][j] = value[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt; quantity; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">    	<span class="comment">// 相关处理</span></span><br><span class="line">    	</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt;= capacity; ++j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">            <span class="keyword">if</span> (j &lt; weight[i])</span><br><span class="line">            &#123;</span><br><span class="line">                dp[i][j] = dp[i - <span class="number">1</span>][j];</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                dp[i][j] = std::<span class="built_in">max</span>(dp[i - <span class="number">1</span>][j], dp[i - <span class="number">1</span>][j - weight[i]] + value[i]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">    cout &lt;&lt; dp[quantity - <span class="number">1</span>][capacity] &lt;&lt; endl; <span class="comment">// 35</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 5. 模拟/举例推导dp数组以验证</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">func</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>二维dp数组可先遍历物品，后<strong>正序</strong>遍历背包：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line">   <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt; quantity; ++i)</span><br><span class="line">   &#123;</span><br><span class="line">   	<span class="comment">// 相关处理</span></span><br><span class="line">   	</span><br><span class="line">       <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt;= capacity; ++j)</span><br><span class="line">       &#123;</span><br><span class="line">           <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">           <span class="keyword">if</span> (j &lt; weight[i])</span><br><span class="line">           &#123;</span><br><span class="line">               dp[i][j] = dp[i - <span class="number">1</span>][j];</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">else</span></span><br><span class="line">           &#123;</span><br><span class="line">               dp[i][j] = std::<span class="built_in">max</span>(dp[i - <span class="number">1</span>][j], dp[i - <span class="number">1</span>][j - weight[i]] + value[i]);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>二维dp数组可先遍历背包，后<strong>正序</strong>遍历物品：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line">   <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt;= capacity; ++j)</span><br><span class="line">   &#123;</span><br><span class="line">   	<span class="comment">// 相关处理</span></span><br><span class="line">   	</span><br><span class="line">       <span class="keyword">for</span> (<span class="type">int</span> i = q; i &lt;= quantity; ++i)</span><br><span class="line">       &#123;</span><br><span class="line">           <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">           <span class="keyword">if</span> (j &lt; weight[i])</span><br><span class="line">           &#123;</span><br><span class="line">               dp[i][j] = dp[i - <span class="number">1</span>][j];</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">else</span></span><br><span class="line">           &#123;</span><br><span class="line">               dp[i][j] = std::<span class="built_in">max</span>(dp[i - <span class="number">1</span>][j], dp[i - <span class="number">1</span>][j - weight[i]] + value[i]);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p> 一维dp数组只能先遍历物品，后<strong>倒序</strong>遍历背包：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">func</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 0. 分析应用问题转换为背包问题 -&gt; 确定背包问题的参数</span></span><br><span class="line">    <span class="type">int</span> capacity = <span class="number">4</span>;                 <span class="comment">// 背包：容量</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; weight = &#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>&#125;;   <span class="comment">// 物体：重量</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; value = &#123;<span class="number">15</span>, <span class="number">20</span>, <span class="number">30</span>&#125;; <span class="comment">// 物体：价值</span></span><br><span class="line">    <span class="type">int</span> quantity = <span class="number">3</span>;                 <span class="comment">// 物体：数量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">    <span class="comment">// 3. 确定dp数组的初始化值</span></span><br><span class="line">    <span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">dp</span><span class="params">(capacity + <span class="number">1</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; quantity; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">    	<span class="comment">// 相关处理</span></span><br><span class="line">    	</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = capacity; j &gt;= weight[i]; --j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">            dp[j] = std::<span class="built_in">max</span>(dp[j], dp[j - weight[i]] + value[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">    cout &lt;&lt; dp[capacity] &lt;&lt; endl; <span class="comment">// 35</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 5. 模拟/举例推导dp数组以验证</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">func</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：还存在三维或更高维dp数组。如：474.一和零 - 力扣（LeetCode）——中等</p>
</blockquote>
<hr>
<h2 id="4-模板示例：完全背包问题"><a href="#4-模板示例：完全背包问题" class="headerlink" title="4. 模板示例：完全背包问题"></a>4. 模板示例：完全背包问题</h2><blockquote>
<p>详细解释参见：<a target="_blank" rel="noopener" href="https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85.html">代码随想录 (programmercarl.com)</a></p>
</blockquote>
<p>一维dp数组：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">func</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 0. 分析应用问题转换为背包问题 -&gt; 确定背包问题的参数</span></span><br><span class="line">    <span class="type">int</span> capacity = <span class="number">4</span>;                 <span class="comment">// 背包：容量</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; weight = &#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>&#125;;   <span class="comment">// 物体：重量</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; value = &#123;<span class="number">15</span>, <span class="number">20</span>, <span class="number">30</span>&#125;; <span class="comment">// 物体：价值</span></span><br><span class="line">    <span class="type">int</span> quantity = <span class="number">3</span>;                 <span class="comment">// 物体：数量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">    <span class="comment">// 3. 确定dp数组的初始化值</span></span><br><span class="line">    <span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">dp</span><span class="params">(capacity + <span class="number">1</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; quantity; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 相关处理</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = weight[i]; j &lt;= capacity; ++j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">            dp[j] = std::<span class="built_in">max</span>(dp[j], dp[j - weight[i]] + value[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 确定dp数组中下标和值的含义 -&gt; 确定dp数组的维度、规模和所求结果/返回值</span></span><br><span class="line">    cout &lt;&lt; dp[capacity] &lt;&lt; endl; <span class="comment">// 60</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 5. 模拟/举例推导dp数组以验证</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">func</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>一维dp数组可先遍历物品，后<strong>正序</strong>遍历背包：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; quantity; ++i)</span><br><span class="line">  &#123;</span><br><span class="line">      <span class="comment">// 相关处理</span></span><br><span class="line"></span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> j = weight[i]; j &lt;= capacity; ++j)</span><br><span class="line">      &#123;</span><br><span class="line">          <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">          dp[j] = std::<span class="built_in">max</span>(dp[j], dp[j - weight[i]] + value[i]);</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>一维dp数组可先遍历背包，后<strong>正序</strong>遍历物品：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 4. 确定dp数组的遍历顺序和临界条件</span></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt;= capacity; ++j)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 相关处理</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; quantity; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 2. 确定dp数组的状态转移方程/递推公式</span></span><br><span class="line">        <span class="keyword">if</span> (j - weight[i] &gt;= <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            dp[j] = std::<span class="built_in">max</span>(dp[j], dp[j - weight[i]] + value[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>若求排列数，则先遍历背包，后遍历物品<br>若求组合数，则先遍历物品，后遍历背包</p>
</blockquote>
<hr>
<h2 id="5-模板示例：多重背包"><a href="#5-模板示例：多重背包" class="headerlink" title="5. 模板示例：多重背包"></a>5. 模板示例：多重背包</h2><blockquote>
<p>详细解释参见：<a target="_blank" rel="noopener" href="https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80%E5%A4%9A%E9%87%8D%E8%83%8C%E5%8C%85.html#%E5%A4%9A%E9%87%8D%E8%83%8C%E5%8C%85">代码随想录 (programmercarl.com</a></p>
</blockquote>
<hr>
<h1 id="九、力扣例题"><a href="#九、力扣例题" class="headerlink" title="九、力扣例题"></a>九、力扣例题</h1><blockquote>
<p>题目组织依据：<a target="_blank" rel="noopener" href="https://programmercarl.com/">代码随想录 (programmercarl.com)</a></p>
</blockquote>
<p>基础问题：</p>
<ul>
<li>509.斐波那契数——简单</li>
<li>70.爬楼梯——简单</li>
<li>746.使用最小花费爬楼梯——简单</li>
<li>62.不同路径——中等</li>
<li>63.不同路径 II——中等</li>
<li>343.整数拆分——中等</li>
<li>96.不同的二叉搜索树——中等</li>
</ul>
<p>背包问题：0-1背包问题</p>
<ul>
<li>416.分割等和子集——中等</li>
<li>1049.最后一块石头的重量 II——中等</li>
<li>494.目标和——中等</li>
<li>474.一和零——中等</li>
</ul>
<p>背包问题：完全背包问题</p>
<ul>
<li>518.零钱兑换 II——中等</li>
<li>377.组合总和 Ⅳ——中等</li>
<li>70.爬楼梯——简单</li>
<li>322.零钱兑换——中等</li>
<li>279.完全平方数——中等</li>
<li>139.单词拆分——中等</li>
</ul>
<p>打家劫舍问题：</p>
<ul>
<li>198.打家劫舍——中等</li>
<li>213.打家劫舍 II——中等</li>
<li>337.打家劫舍 III——中等</li>
</ul>
<p>股票问题：</p>
<ul>
<li>121.买卖股票的最佳时机——简单</li>
<li>122.买卖股票的最佳时机 II——中等</li>
<li>123.买卖股票的最佳时机 III——困难</li>
<li>188.买卖股票的最佳时机 IV——困难</li>
<li>309.最佳买卖股票时机含冷冻期——中等</li>
<li>714.买卖股票的最佳时机含手续费——中等</li>
</ul>
<p>子序列问题：非连续和连续子序列问题</p>
<ul>
<li>300.最长递增子序列——中等</li>
<li>674.最长连续递增序列——简单</li>
<li>718.最长重复子数组——中等</li>
<li>1143.最长公共子序列——中等</li>
<li>1035.不相交的线——中等</li>
<li>53.最大子数组和——中等</li>
</ul>
<p>子序列问题：编辑距离问题</p>
<ul>
<li>392.判断子序列——简单</li>
<li>115.不同的子序列——困难</li>
<li>583.两个字符串的删除操作——中等</li>
<li>72.编辑距离——困难</li>
</ul>
<p>子序列问题：回文问题</p>
<ul>
<li>647.回文子串——中等</li>
<li>516.最长回文子序列——中等</li>
</ul>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>动态规划（Dynamic Programming）（dp）描述。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li><a target="_blank" rel="noopener" href="https://programmercarl.com/">代码随想录 (programmercarl.com)</a></li>
<li>《代码随想录》作者：孙秀洋</li>
<li><a target="_blank" rel="noopener" href="https://leetcode.cn/">力扣（LeetCode）官网 - 全球极客挚爱的技术成长平台</a></li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/02/01/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%88Dynamic%20Programming%EF%BC%89%EF%BC%88dp%EF%BC%89%E6%8F%8F%E8%BF%B0/">http://example.com/2023/02/01/动态规划（Dynamic Programming）（dp）描述/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%AE%97%E6%B3%95/">算法</a></div><div class="post_share"><div class="social-share" data-image="/img/cover18.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/01/16/%E3%80%8A%E6%95%B0%E6%8D%AE%E5%BA%93%E5%8E%9F%E7%90%86%E5%8F%8A%E5%BA%94%E7%94%A8%E6%95%99%E7%A8%8B%EF%BC%88%E7%AC%AC4%E7%89%88%20_%20%E5%BE%AE%E8%AF%BE%E7%89%88%EF%BC%89%E3%80%8B%E7%AC%AC3%E7%AB%A0%EF%BC%9A%E2%80%9C%E5%85%B3%E7%B3%BB%E6%95%B0%E6%8D%AE%E5%BA%93%E6%A0%87%E5%87%86%E8%AF%AD%E8%A8%80%E2%80%94%E2%80%94SQL%E2%80%9D%E5%92%8C%E7%AC%AC7%E7%AB%A0%EF%BC%9A%E2%80%9CSQL%20Server%202012%E9%AB%98%E7%BA%A7%E5%BA%94%E7%94%A8%E2%80%9D%E7%9A%84%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2/" title="《数据库原理及应用教程（第4版 | 微课版）》第3章：“关系数据库标准语言——SQL”和第7章：“SQL Server 2012高级应用”的学习提纲"><img class="cover" src= "" data-lazy-src="/img/cover3.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">《数据库原理及应用教程（第4版 | 微课版）》第3章：“关系数据库标准语言——SQL”和第7章：“SQL Server 2012高级应用”的学习提纲</div></div></a></div><div class="next-post pull-right"><a href="/2023/02/13/%E5%9B%9E%E6%BA%AF%EF%BC%88backtrack%EF%BC%89%E6%8F%8F%E8%BF%B0/" title="回溯（backtrack）描述"><img class="cover" src= "" data-lazy-src="/img/cover9.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">回溯（backtrack）描述</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/02/13/%E5%9B%9E%E6%BA%AF%EF%BC%88backtrack%EF%BC%89%E6%8F%8F%E8%BF%B0/" title="回溯（backtrack）描述"><img class="cover" src= "" data-lazy-src="/img/cover9.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-13</div><div class="title">回溯（backtrack）描述</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5"><span class="toc-number">2.</span> <span class="toc-text">一、核心概念</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%85%B8%E5%9E%8B%E9%97%AE%E9%A2%98"><span class="toc-number">3.</span> <span class="toc-text">二、典型问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E7%AE%97%E6%B3%95%E6%AD%A5%E9%AA%A4"><span class="toc-number">4.</span> <span class="toc-text">三、算法步骤</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E6%A8%A1%E6%9D%BF%E7%A4%BA%E4%BE%8B"><span class="toc-number">5.</span> <span class="toc-text">四、模板示例</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E8%B0%83%E8%AF%95%E6%96%B9%E6%B3%95"><span class="toc-number">6.</span> <span class="toc-text">五、调试方法</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90"><span class="toc-number">7.</span> <span class="toc-text">六、性能分析</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E4%BC%98%E5%8C%96%E6%96%B9%E6%B3%95"><span class="toc-number">8.</span> <span class="toc-text">七、优化方法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E8%AE%B0%E5%BF%86%E5%8C%96%E6%90%9C%E7%B4%A2"><span class="toc-number">8.1.</span> <span class="toc-text">1. 记忆化搜索</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%BB%9A%E5%8A%A8%E6%95%B0%E7%BB%84"><span class="toc-number">8.2.</span> <span class="toc-text">2. 滚动数组</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98"><span class="toc-number">9.</span> <span class="toc-text">八、背包问题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E7%B1%BB%E5%9E%8B"><span class="toc-number">9.1.</span> <span class="toc-text">1. 类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E7%AE%97%E6%B3%95%E6%AD%A5%E9%AA%A4"><span class="toc-number">9.2.</span> <span class="toc-text">2. 算法步骤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%A8%A1%E6%9D%BF%E7%A4%BA%E4%BE%8B%EF%BC%9A0-1%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98"><span class="toc-number">9.3.</span> <span class="toc-text">3. 模板示例：0-1背包问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E6%A8%A1%E6%9D%BF%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98"><span class="toc-number">9.4.</span> <span class="toc-text">4. 模板示例：完全背包问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E6%A8%A1%E6%9D%BF%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%A4%9A%E9%87%8D%E8%83%8C%E5%8C%85"><span class="toc-number">9.5.</span> <span class="toc-text">5. 模板示例：多重背包</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B9%9D%E3%80%81%E5%8A%9B%E6%89%A3%E4%BE%8B%E9%A2%98"><span class="toc-number">10.</span> <span class="toc-text">九、力扣例题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">11.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">12.</span> <span class="toc-text">参考资料</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover18.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>